package com.atmilan.quartz.job;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import com.atmilan.quartz.constant.Constants;
import com.atmilan.quartz.constant.enums.SyncStatusEnum;
import com.atmilan.quartz.model.entity.crmeb.SystemConfig;
import com.atmilan.quartz.service.IEfasionSyncService;
import com.atmilan.quartz.service.ISystemConfigService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xxl.job.core.handler.annotation.XxlJob;

import lombok.extern.slf4j.Slf4j;

/**
 * 易时尚商品同步
 * @author daigb
 * @date:  2022-06-14 17:27
 */
@Slf4j
@Component
@Configuration
@EnableScheduling
public class EfasionSyncTask {
    @Autowired
    BaseTask baseTask;
    @Autowired
    IEfasionSyncService efasionSyncService;
    @Autowired
    ISystemConfigService systemConfigService;
    
    @XxlJob("efasionSyncTask")
    public void syncTask() {
        try {
            SystemConfig systemConfig = systemConfigService.getOne(Wrappers.<SystemConfig>lambdaQuery()
                    .eq(SystemConfig::getName, Constants.EFASION_SYNC_STATUS)
                    .last("LIMIT 1")
                    );
            SyncStatusEnum efasionSyncStatus = SyncStatusEnum.valueOf(systemConfig.getFormId());
            long timeDiff = new Date().getTime() - systemConfig.getUpdateTime().getTime();

            log.warn("--------------- 开始计划同步efasion商品, 同步状态为: {} ---------------", efasionSyncStatus);
            // 12小时内状态还未变更, 直接重新处理
            if (SyncStatusEnum.WAIT == efasionSyncStatus && 12 < (timeDiff / 1000 / 60 / 60)) {
                log.warn("--------------- efasion商品同步过程超过12小时, 自动重新拉取数据 ---------------");
                efasionSyncStatus = SyncStatusEnum.PUSH;
            }
            // 有同步任务时不进行处理
            if (SyncStatusEnum.WAIT == efasionSyncStatus) {
                log.warn("--------------- efasion任务未结束, 新任务不执行 ---------------");
                return;
            }
            // 更新任务状态为同步中
            baseTask.updateSyscStatus(Constants.EFASION_SYNC_STATUS, SyncStatusEnum.WAIT, systemConfig);
            // 下发完成, 需要同步
            if (SyncStatusEnum.PUSH == efasionSyncStatus) {
            	boolean result = efasionSyncService.syncProduct();
                if (result) {
                	baseTask.updateSyscStatus(Constants.EFASION_SYNC_STATUS, SyncStatusEnum.SYNC, systemConfig);
                    log.warn("--------------- efasion商品拉取完成!!! ---------------");
                }
                // 同步完成, 需要下发
			} else if (SyncStatusEnum.SYNC == efasionSyncStatus) {
				boolean result = efasionSyncService.pushProduct();
				if (result) {
					baseTask.updateSyscStatus(Constants.EFASION_SYNC_STATUS, SyncStatusEnum.PUSH, systemConfig);
			    	log.warn("--------------- efasion商品下发完成!!! ---------------");
			   }
			}

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
